<template>
	<div>
		<ProTable ref="proTable" title="价格趋势" :columns="columns" :requestApi="getTableList" :init-param="initParam">
			<!-- Expand -->
			<template #expand="scope">
				{{ scope.row }}
			</template>
			<!-- 表格操作 -->
			<template #operation="{ row }">
				<el-button
					type="primary"
					link
					@click="maintainPrice(row)"
					v-auth-all="['product:price:trend:detail:invoke', 'product:price:trend:maintenance:invoke']"
				>
					维护价格趋势
				</el-button>
			</template>
		</ProTable>
	</div>
</template>

<script setup lang="tsx" name="systemOperation">
import { ref, reactive, computed } from 'vue';
import { useRouter } from 'vue-router';
import ProTable from '../../../../components/ProTable/index.vue';
import { ProTableInstance, ColumnProps } from '../../../../components/ProTable/interface';
import { fetchGoodList, fetchTypeList } from '/@/api/goods';
import { Session } from '/@/utils/storage';
import { fenToYuan } from '/@/utils/commonFunction';

// 获取 ProTable 元素，调用其nm获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref<ProTableInstance>();

const router = useRouter();

const searchArr = [
	{
		label: '商品ID',
		value: 'id',
	},
	{
		label: '物料编码',
		value: 'materialCode',
	},
	{
		label: '商品名称',
		value: 'name',
	},
];

// 如果表格需要初始化请求参数，直接定义传给 ProTable(之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
const initParam = reactive({
	paramCode: 'id',
});

const handleParams = (params: any) => {
	let data = {
		...params,
		status: '',
	};
	if (data.paramValue) {
		data[data.paramCode] = data.paramValue;
	}
	delete data.paramValue;
	delete data.paramCode;

	return data;
};

// 如果你想在请求之前对当前请求参数做一些操作，可以自定义如下函数：params 为当前所有的请求参数（包括分页），最后返回请求列表接口
// 默认不做操作就直接在 ProTable 组件上绑定	:requestApi="getTableList"
const getTableList = (params: any) => {
	const data = handleParams(params);
	return fetchGoodList(data);
};

const columns: ColumnProps<any>[] = [
	{
		prop: 'paramValue',
		label: '商品搜索',
		isShow: false,
		search: {
			span: 2,
			xs: 1,
			sm: 1,
			md: 2,
			lg: 1,
			xl: 1,
			render: (scope: any) => {
				const slot = {
					prepend: () => (
						<el-select
							v-model={scope.searchParam.paramCode}
							placeholder="Select"
							style="width: 100px"
							onChange={(value) => (scope.searchParam.paramCode = value)}
						>
							{searchArr.map((item) => (
								<el-option key={item.value} label={item.label} value={item.value} />
							))}
						</el-select>
					),
				};
				return <el-input v-model={scope.modelValue} v-slots={slot} placeholder={getPlaceholder.value} maxlength={50}></el-input>;
			},
		},
	},
	{
		prop: 'id',
		label: '商品ID',
		minWidth: 120,
	},
	{
		prop: 'name',
		label: '商品名称',
		minWidth: 160,
		render: (scope) => {
			return (
				<div class="flex-center">
					<el-image class="flex-shrink" src={scope.row.productImage} style="width:50px;height:50px"></el-image>
					<div class="ml10" style="text-align:left">
						<div>{scope.row.productName}</div>
						<div>{scope.row.productCode}</div>
					</div>
				</div>
			);
		},
	},
	{
		prop: 'type',
		label: '商品类型',
		minWidth: 120,
		search: { el: 'select', props: { placeholder: '请选择商品类型', clearable: true, filterable: true } },
		enum: [
			{
				label: '实物',
				value: 0,
			},
			{
				label: '酒藏',
				value: 1,
			},
		],
		render(scope) {
			return scope.row.productType === 0 ? '实物' : '酒藏';
		},
	},
	{
		prop: 'productTypeId',
		label: '商品分类',
		minWidth: 120,
		search: { el: 'select', props: { placeholder: '请选择商品分类', clearable: true, filterable: true } },
		fieldNames: { label: 'name', value: 'id' },
		enum: () => {
			return new Promise<any>((resolve) => {
				fetchTypeList({ page: 1, size: 2000 }).then((res: any) => {
					resolve({
						code: 200,
						msg: 'success',
						data: res.data.records,
					});
				});
			});
		},
		render(scope) {
			return scope.row.productTypeName;
		},
	},
	{
		prop: 'status',
		label: '商品状态',
		minWidth: 120,
		render(scope: any) {
			const tags = [
				{
					label: '销售中',
					name: '0',
				},
				{
					label: '仓库中',
					name: '1',
				},
				{
					label: '待业务审核',
					name: '2',
				},
				{
					label: '待财务审核',
					name: '3',
				},
				{
					label: '审核未通过',
					name: '4',
				},
			];
			return (
				tags.find((item: any) => {
					return item.name == scope.row.status;
				})?.label || ''
			);
		},
	},
	{
		prop: 'recommended',
		label: '是否推荐',
		minWidth: 120,
		render: (scope) => {
			return scope.row.recommended === 1 ? '是' : '否';
		},
	},
	{
		prop: 'price',
		label: '价格',
		minWidth: 120,
		render: (scope) => {
			const price = scope.row.price?.split('-');
			if (!price) {
				return '--';
			}
			return `${fenToYuan(price[0])}${price[1] ? `-${fenToYuan(price[1])}` : ''}`;
		},
	},
	{
		prop: 'inventory',
		label: '可售库存',
		minWidth: 120,
	},
	{ prop: 'operation', label: '操作', fixed: 'right', width: 140, align: 'left' },
];
const columns1: ColumnProps<any>[] = [
	{
		prop: 'paramValue',
		label: '商品搜索',
		isShow: false,
		search: {
			span: 2,
			xs: 1,
			sm: 1,
			md: 2,
			lg: 1,
			xl: 1,
			render: (scope: any) => {
				const slot = {
					prepend: () => (
						<el-select
							v-model={scope.searchParam.paramCode}
							placeholder="Select"
							style="width: 100px"
							onChange={(value) => (scope.searchParam.paramCode = value)}
						>
							{searchArr.map((item) => (
								<el-option key={item.value} label={item.label} value={item.value} />
							))}
						</el-select>
					),
				};
				return <el-input v-model={scope.modelValue} v-slots={slot} placeholder={getPlaceholder.value} maxlength={50}></el-input>;
			},
		},
	},
	{
		prop: 'id',
		label: '商品ID',
		minWidth: 120,
	},
	{
		prop: 'name',
		label: '商品名称',
		minWidth: 160,
		render: (scope) => {
			return (
				<div class="flex-center">
					<el-image class="flex-shrink" src={scope.row.productImage} style="width:50px;height:50px"></el-image>
					<div class="ml10" style="text-align:left">
						<div class="ellipsis2">{scope.row.productName}</div>
						<div>物料编码：{scope.row.materialCode}</div>
					</div>
				</div>
			);
		},
	},
	{
		prop: 'type',
		label: '商品类型',
		minWidth: 120,
		search: { el: 'select', props: { placeholder: '请选择商品类型', clearable: true, filterable: true } },
		enum: [
			{
				label: '实物',
				value: 0,
			},
			{
				label: '酒藏',
				value: 1,
			},
		],
		render(scope) {
			return scope.row.productType === 0 ? '实物' : '酒藏';
		},
	},
	{
		prop: 'productTypeId',
		label: '商品分类',
		minWidth: 120,
		search: { el: 'select', props: { placeholder: '请选择商品分类', clearable: true, filterable: true } },
		fieldNames: { label: 'name', value: 'id' },
		enum: () => {
			return new Promise<any>((resolve) => {
				fetchTypeList({ page: 1, size: 2000 }).then((res: any) => {
					resolve({
						code: 200,
						msg: 'success',
						data: res.data.records,
					});
				});
			});
		},
		render(scope) {
			return scope.row.productTypeName;
		},
	},
	{
		prop: 'status',
		label: '商品状态',
		minWidth: 120,
		render(scope: any) {
			return (
				tags.value.find((item: any) => {
					return item.name == scope.row.status;
				})?.label || ''
			);
		},
	},
	{
		prop: 'recommended',
		label: '是否推荐',
		minWidth: 120,
		render: (scope) => {
			return scope.row.recommended === 1 ? '是' : '否';
		},
	},
	{
		prop: 'price',
		label: '价格',
		minWidth: 120,
		render: (scope) => {
			const price = scope.row.price?.split('-');
			if (!price) {
				return '--';
			}
			return `${fenToYuan(price[0])}${price[1] ? `-${fenToYuan(price[1])}` : ''}`;
		},
	},
	{
		prop: 'inventory',
		label: '库存',
		minWidth: 120,
	},
	{ prop: 'operation', label: '操作', fixed: 'right', width: 380, align: 'left' },
];

const getPlaceholder = computed(() => {
	const target = searchArr.find((item) => {
		return item.value == proTable.value?.searchParam.paramCode;
	});
	return target ? `输入${target?.label}搜索` : '';
});

// 维护价格
const maintainPrice = (row: any) => {
	Session.set('goodInfo', row);
	router.push({ path: `/goods/price/detail/${row.id}` });
};
</script>

<style lang="scss">
.demo-tabs {
	.el-tabs__header {
		margin-bottom: 0;
	}
}
</style>
